package com.magictower.primary.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.magictower.primary.R;
import com.magictower.primary.common.SPConstant;
import com.magictower.primary.view.ClickView;
import com.magictower.primary.view.ItemView;

public class SettingActivity extends AppCompatActivity {

    private SharedPreferences mPref;
    private ItemView updateItem;
    private ItemView addressShow;
    private ClickView addressStyle;
    private ClickView addressLocation;
    private ItemView blackName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        mPref = getSharedPreferences(SPConstant.CONFIG, MODE_PRIVATE);
        initAutoUpdate();//自动升级设置
        initAddress();//归属地显示
        initAddressStyle();//归属地样式
        initBlackCall();//黑名单设置
        initAddressLocation();//归属地显示位置
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        addressLocation = (ClickView) findViewById(R.id.scv_address_location);
        addressLocation.setDesc("横坐标：" + mPref.getInt(SPConstant.TOAST_X, 0)
                + "；纵坐标：" + mPref.getInt(SPConstant.TOAST_Y, 0));
    }

    private void initAddressLocation() {
        addressLocation = (ClickView) findViewById(R.id.scv_address_location);
        int toastX = mPref.getInt(SPConstant.TOAST_X, 0);
        int toastY = mPref.getInt(SPConstant.TOAST_Y, 0);
        addressLocation.setTitle("归属地显示坐标");
        addressLocation.setDesc("横坐标：" + toastX + "；纵坐标：" + toastY);
        addressLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SettingActivity.this, DragViewActivity.class));
            }
        });
    }

    private void initBlackCall() {
        blackName = (ItemView) findViewById(R.id.siv_black_name);
        Boolean show = mPref.getBoolean(SPConstant.BLACK_NAME, true);
        if (show) {
            //开启
            blackName.setCheck(true);
        } else {
            //关闭
            blackName.setCheck(false);
        }
        blackName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (blackName.isCheck()) {
                    blackName.setCheck(false);
                    mPref.edit().putBoolean(SPConstant.BLACK_NAME, false).apply();
                } else {
                    blackName.setCheck(true);
                    mPref.edit().putBoolean(SPConstant.BLACK_NAME, true).apply();
                }
            }
        });
    }

    private void initAddressStyle() {
        addressStyle = (ClickView) findViewById(R.id.scv_address_style);
        int style = mPref.getInt(SPConstant.TOAST_STYLE, 0);
        addressStyle.setTitle("归属地提示框风格");
        addressStyle.setDesc(items[style]);
        addressStyle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSingleChooseDialog();
            }
        });
    }

    private void initAddress() {
        addressShow = (ItemView) findViewById(R.id.siv_address);
        Boolean show = mPref.getBoolean(SPConstant.ADDRESS_SHOW, true);
        if (show) {
            //开启
            addressShow.setCheck(true);
        } else {
            //关闭
            addressShow.setCheck(false);
        }
        addressShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (addressShow.isCheck()) {
                    addressShow.setCheck(false);
                    mPref.edit().putBoolean(SPConstant.ADDRESS_SHOW, false).apply();
                } else {
                    addressShow.setCheck(true);
                    mPref.edit().putBoolean(SPConstant.ADDRESS_SHOW, true).apply();
                }
            }
        });
    }

    private void initAutoUpdate() {
        updateItem = (ItemView) findViewById(R.id.siv_update);
        Boolean autoUpdate = mPref.getBoolean(SPConstant.AUTO_UPDATE, false);
        if (autoUpdate) {
            //开启
            updateItem.setCheck(true);
        } else {
            //关闭
            updateItem.setCheck(false);
        }
        updateItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (updateItem.isCheck()) {
                    updateItem.setCheck(false);
                    mPref.edit().putBoolean(SPConstant.AUTO_UPDATE, false).apply();
                } else {
                    updateItem.setCheck(true);
                    mPref.edit().putBoolean(SPConstant.AUTO_UPDATE, true).apply();
                }
            }
        });
    }

    final String[] items = SPConstant.ITEMS;
    /**
     * 弹出选择风格的单选框
     */
    protected void showSingleChooseDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("归属地提示框风格");
        int style = mPref.getInt(SPConstant.TOAST_STYLE, 0);//存储的风格

        builder.setSingleChoiceItems(items, style, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mPref.edit().putInt(SPConstant.TOAST_STYLE, which).apply();
                dialog.dismiss();
                addressStyle.setDesc(items[which]);
            }
        });
        builder.setNegativeButton("取消",null);
        builder.show();

    }
}
